package com.xyebank.ledger.service.nx.adaper;

import org.modelmapper.ModelMapper;
import org.modelmapper.PropertyMap;
import org.springframework.stereotype.Component;

import com.xyebank.ledger.db.entitys.LApplicationInf;
import com.xyebank.ledger.service.common.conveter.ApplyBeanConverter;
import com.xyebank.ledger.service.constant.CommonConstant;
import com.xyebank.ledger.service.constant.ProductConstant;
import com.xyebank.ledger.util.DateConverter;
import com.xyebank.ledger.util.IDUtil;
import com.xyebank.ledger.util.StatusConvertUtil;
import com.xyebank.ledger.vo.deal.LNxCashApplicationInfVo;

@Component(value = "nXApplyBeanConverter")
public class NXApplyBeanConverter implements ApplyBeanConverter<LNxCashApplicationInfVo> {

    // 不能采取注入方式
    private DateConverter dateConverter = new DateConverter();
    
    ModelMapper modelMapper = new ModelMapper();

    class NXApplyPropertyMap extends PropertyMap<LNxCashApplicationInfVo, LApplicationInf> {
        protected void configure() { //注意　不要有重复的映射，不然会报错
        	map(source.getOrdNo(), destination.getXyOrderNo());
        	map(source.getMblNo(), destination.getMblNo());
        	using(dateConverter).map(source.getApplTime(), destination.getApplyTime()); //　日期格式转换错误
        	map(source.getCreditTime(), destination.getCreditTime());
            map(source.getAccLim(), destination.getCreditAmount());
            map(source.getLoanTerm(), destination.getLoanTerm());
            map(source.getAppralRes(), destination.getStatus());
        }
    }

    public NXApplyBeanConverter() {
        modelMapper.addMappings(new NXApplyPropertyMap());
    }

    @Override
    public LApplicationInf covert(LNxCashApplicationInfVo x) {
        LApplicationInf entity = modelMapper.map(x, LApplicationInf.class);
        entity.setId(IDUtil.generateId(x.getId().toString(), "nx"));
        entity.setDataSource(x.getSubOrgNo().equals("01") ? CommonConstant.JRY_STR : CommonConstant.SJD_STR);
        entity.setXyOrgNo(ProductConstant.NX_LOAN_ORG);
        entity.setXyProductNo(ProductConstant.NX_LOAN_PROD_ID);
        if(x.getStatus() != null)entity.setStatus(StatusConvertUtil.convertApplyStatus(x.getStatus()));
        return entity;
    }
}
